package com.lanou.controller;

import com.lanou.pojo.PaymentMeterEntity;
import com.lanou.service.PaymentMeterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * @ClassName PaymentMeterController
 * @Description: TODO
 * @Author 郅占伟
 * @Date 2023年10月27日
 * @Version: 1.0
 */
@Api(value = "抄表数据管理", tags = "抄表数据管理")
@RestController
public class PaymentMeterController {
    @Autowired
    private PaymentMeterService paymentMeterService;

    @ApiOperation(value = "获取数据", httpMethod = "GET")
    @RequestMapping(value = "getPaymentMeterList", produces = "text/html;charset=UTF-8")
    public String getPaymentMeterList(String pageNum, String pageSize, String blockCode, String feeItemName) {
        return paymentMeterService.getPaymentMeterList(pageNum, pageSize, blockCode, feeItemName);
    }

    // 下载模板
    @ApiOperation(value = "下载模板", httpMethod = "GET")
    @RequestMapping(value = "/downloadTemplate", produces = "text/html;charset=utf-8")
    public ResponseEntity<FileSystemResource> downloadExcel() {
        String path = "/opt/tomcat/apache-tomcat-9.0.29/webapps/ROOT/WEB-INF/classes/" + "抄表数据导入模板.xlsx";
        File file = new File(path);
        FileSystemResource resource = new FileSystemResource(file);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", "抄表数据导入模板.xlsx");
        return ResponseEntity.ok().headers(headers).body(resource);
    }

    // 上传的文件
    @ApiOperation(value = "上传Excel文件", httpMethod = "POST")
    @RequestMapping(value = "/upload", produces = "text/html;charset=utf-8")
    public String getUploadFile(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        return paymentMeterService.uploadFile(multipartFile);
    }


    // 分析文件存储到数据库
    @ApiOperation(value = "分析Excel文件到数据库", httpMethod = "GET")
    @RequestMapping("/analyseExcelFile")
    public void analyseExcelFile(String filePath) throws Exception {
        paymentMeterService.analyseFileToDb(filePath);
    }

    // 删除文件
    @ApiOperation(value = "通过行id删除数据", httpMethod = "GET")
    @RequestMapping("/deletePaymentMeterById")
    public void deletePaymentMeterById(String id){
        paymentMeterService.deletePaymentMeterById(id);
    }

    // 更新数据
    @ApiOperation(value = "修改表单数据",httpMethod = "POST")
    @RequestMapping("/modifyPaymentMeter")
    public void modifyPaymentMeter(@RequestBody PaymentMeterEntity paymentMeterEntity){
        paymentMeterService.modifyPaymentMeter(paymentMeterEntity);
    }
}
